using System.Collections.Generic;
using System.IO;
using Common;

namespace SProject.Rule
{
	public class AQRule : Rule
	{
		private SimpleRule _ExperimentICRule = new SimpleRule();

		private SimpleRule _SampleICRule = new SimpleRule();

		private SimpleRule _ResuleRule = new SimpleRule();

		public SimpleRule ExperimentICRule
		{
			get
			{
				return _ExperimentICRule;
			}
			set
			{
				if (_ExperimentICRule != value)
				{
					_ExperimentICRule = value;
					OnPropertyChanged("ExperimentICRule");
				}
			}
		}

		public SimpleRule SampleICRule
		{
			get
			{
				return _SampleICRule;
			}
			set
			{
				if (_SampleICRule != value)
				{
					_SampleICRule = value;
					OnPropertyChanged("SampleICRule");
				}
			}
		}

		public SimpleRule ResuleRule
		{
			get
			{
				return _ResuleRule;
			}
			set
			{
				if (_ResuleRule != value)
				{
					_ResuleRule = value;
					OnPropertyChanged("ResuleRule");
				}
			}
		}

		public override Rule CreateRule()
		{
			return new AQRule();
		}

		public override void Copy(Rule rule)
		{
			base.Copy(rule);
			if (rule is AQRule)
			{
				ExperimentICRule = ((AQRule)rule).ExperimentICRule.Clone();
				SampleICRule = ((AQRule)rule).SampleICRule.Clone();
				ResuleRule = ((AQRule)rule).ResuleRule.Clone();
			}
		}

		protected override void GetData(List<PropertyKey> keys, List<byte[]> datas)
		{
			base.GetData(keys, datas);
			StreamEx streamEx = new StreamEx();
			keys.Add(PropertyKey.Key_ExperimentICRule);
			ExperimentICRule.SaveToStream(streamEx);
			datas.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			keys.Add(PropertyKey.Key_SampleICRule);
			SampleICRule.SaveToStream(streamEx);
			datas.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			keys.Add(PropertyKey.Key_ResuleRule);
			ResuleRule.SaveToStream(streamEx);
			datas.Add(streamEx.ToArray());
			streamEx.Close();
		}

		protected override void SetData(PropertyKey key, byte[] data)
		{
			base.SetData(key, data);
			switch (key)
			{
			case PropertyKey.Key_ExperimentICRule:
			{
				StreamEx streamEx = new StreamEx();
				streamEx.Write(data);
				streamEx.Seek(0L, SeekOrigin.Begin);
				ExperimentICRule.ReadFromStream(streamEx);
				streamEx.Close();
				break;
			}
			case PropertyKey.Key_SampleICRule:
			{
				StreamEx streamEx = new StreamEx();
				streamEx.Write(data);
				streamEx.Seek(0L, SeekOrigin.Begin);
				SampleICRule.ReadFromStream(streamEx);
				streamEx.Close();
				break;
			}
			case PropertyKey.Key_ResuleRule:
			{
				StreamEx streamEx = new StreamEx();
				streamEx.Write(data);
				streamEx.Seek(0L, SeekOrigin.Begin);
				ResuleRule.ReadFromStream(streamEx);
				streamEx.Close();
				break;
			}
			}
		}

		public override void SaveToStream(StreamEx stream)
		{
			base.SaveToStream(stream);
		}

		public override bool ReadFromStream(StreamEx stream)
		{
			if (base.ReadFromStream(stream))
			{
				while (true)
				{
					switch (stream.ReadKey())
					{
					case PropertyKey.Key_ExperimentICRule:
						ExperimentICRule.ReadFromStream(stream);
						break;
					case PropertyKey.Key_SampleICRule:
						SampleICRule.ReadFromStream(stream);
						break;
					case PropertyKey.Key_ResuleRule:
						ResuleRule.ReadFromStream(stream);
						break;
					default:
						stream.Seek(-4L, SeekOrigin.Current);
						return true;
					}
				}
			}
			return false;
		}
	}
}
